<?php

//管理员
class AdminAction extends Action {
	
	private $cLogs = null;
	
	function __construct(){
		//判断管理员登录状态
		admin_login_check();
		
		import('@.ORG.Get');
		import('@.ORG.Post');
		import('@.ORG.Logs');
		
		
		$this->cLogs = new Logs();
	}
	
	//列表
    public function index(){

    	admin_role('Admin:index');
    	
    	import('@.ORG.Admin');
    	$cAdmin = new Admin();
    	
    	$admins = $cAdmin->lists();
        	
    	$this->assign(
    		array(
    			'admins'		=> $admins,
    			'MainPage'		=> 'Admin:index'
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function add(){

    	admin_role('Admin:add');
    	
    	//权限列表
    	import('@.ORG.Roles');
    	$cRoles = new Roles();
    	
    	$roles  = $cRoles->lists();

    	
    	//分组列表
    	import('@.ORG.AdminGroups');
    	$cAdminGroups = new AdminGroups();
    	
    	$groups  = $cAdminGroups->lists();
    	
    	
    	$this->assign(
    		array(
    			'MainPage'	=> 'Admin:add',
    			'roles' 	=> $roles,
    			'groups'	=> $groups
    		)
    	);
    	$this->display(C('ADM_TEMPLATE'));
    }
    

    public function add_do(){

    	admin_role('Admin:add');
    	
    	$name 			= $this->_post('name'			,'','');
    	$passwd 		= $this->_post('passwd'			,'','');
    	$repasswd 		= $this->_post('repasswd'		,'','');
    	$real_name 		= $this->_post('real_name'		,'','');
    	$email	 		= $this->_post('email'			,'','');
    	$mobile_phone 	= $this->_post('mobile_phone'	,'','');
    	$remark 		= $this->_post('remark'			,'','');
    	$group_id		= $this->_post('group_id'		,'intval','0');
    	$role 			= $this->_post('role'			,'','');
    	$disabled 		= $this->_post('disabled'		,'',0);
    	
		import('@.ORG.Admin');
    	$cAdmin = new Admin();
    	
    	$data = array(
    		'name' 			=> $name,
	    	'real_name' 	=> $real_name,
    		'email'			=> $email,
	    	'mobile_phone' 	=> $mobile_phone,
	    	'remark' 		=> $remark,
    		'group_id'		=> $group_id,
	    	'role' 			=> $role
    	);
    	
    	$add_result = $cAdmin->add($name,$passwd,$data);

    	if($add_result){
    		
    		$this->cLogs->add('Admin:add','管理员添加成功:'.$real_name);
    		
    		json_success('管理员添加成功');
    	}else{
    		json_error('管理员添加失败');
    	}
    }
    
    
    
    //详情或修改
    public  function detail(){
    	
    	admin_role('Admin:edit');
    	
    	$id 	= $this->_get('id'	,'',0);
				
    	import('@.ORG.Admin');
    	$cAdmin = new Admin();
    	
    	//管理员详情
    	$admin = $cAdmin->get_by_id($id);
    	
    	
    	if($admin['name'] === 'admin'){
    		redirect('/index.php/Admin/Admin/',1,'超级管理员不能修改');
    	}
    	
    	//权限列表
    	import('@.ORG.Roles');
    	$cRoles = new Roles();
    	
    	$roles  = $cRoles->lists();
    	    	
    	
    	//分组列表
    	import('@.ORG.AdminGroups');
    	$cAdminGroups = new AdminGroups();
    	
    	$groups  = $cAdminGroups->lists();
    	
    	
    	//判断哪个有权限
    	$admin_roles = explode(',',$admin['role']);
    	
    	foreach ($roles as $i => $r){
    		$roles[$i]['has_role'] = in_array($r['role_value'], $admin_roles);
    	}
    	    	
    	$this->assign(
    		array(
    			'APP_NAME'	=> APP_NAME,
    			'a'		=> $admin,
    			'roles' => $roles,
    			'groups'=> $groups,
    			'MainPage'	=> 'Admin:detail'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    public function update(){
    	
    	admin_role('Admin:edit');
    	
    	$id 			= $this->_post('id'				,'',0);
    	$name 			= $this->_post('name'			,'','');
    	$real_name 		= $this->_post('real_name'		,'','');
    	$email	 		= $this->_post('email'			,'','');
    	$mobile_phone 	= $this->_post('mobile_phone'	,'','');
    	$remark 		= $this->_post('remark'			,'','');
    	$group_id		= $this->_post('group_id'		,'intval','0');
    	$role 			= $this->_post('role'			,'','');
    	$disabled 		= $this->_post('disabled'		,'',0);
    	
    	import('@.ORG.Admin');
    	$cAdmin = new Admin();
    	
    	$upItems = array(
    		'name'			=> $name,
    		'real_name'		=> $real_name,
    		'email'			=> $email,
    		'mobile_phone'	=> $mobile_phone,
    		'remark'		=> $remark,
    		'group_id'		=> $group_id,
    		'role'			=> $role,
    		'disabled'		=> $disabled
    	);
    	
    	$update_result = $cAdmin->update($id,$upItems);
    	    	
    	if($update_result){
    		
    		$this->cLogs->add('Admin:edit','修改管理员:'.$real_name);
    		
    		json_success('管理员修改成功');
    	}else{
    		json_error('管理员修改失败');
    	}    
    }
    
    
 	//详情或修改
    public  function update_pwd(){
    	
    	admin_role('Admin:update_pwd');
    	
    	$id 	= $this->_get('id'	,'',0);
				
    	import('@.ORG.Admin');
    	$cAdmin = new Admin();
    	
    	//管理员详情
    	$admin = $cAdmin->get_by_id($id);
    	
    	if(!$admin){
    		redirect('/index.php/Admin/Admin/',1,'管理员不存在');
    	}
    	
    	//权限
    	$admin_id = admin_id_ajax(); 
    	$current_admin = $cAdmin->get_by_id($admin_id);
    	$role = $current_admin['role'];
    	    	
    	
    	//是否是自己
    	$is_self = 0;
    	if($admin_id == $id){
    		$is_self = 1;
    	}else{
    		if(!($role === 'all')){
    			redirect('/index.php/Admin/Admin/',1,'只有超管理员才能修改他人密码');
    		}
    	}
    	    	
    	$this->assign(
    		array(
    			'APP_NAME'			=> APP_NAME,
    			'id'				=> $id,
    			'is_self'			=> $is_self,
    			'MainPage'			=> 'Admin:update_pwd'
    		)
    	);
    	
    	$this->display(C('ADM_TEMPLATE'));
    }
    
    
    public function update_pwd_do(){
    	
    	admin_role('Admin:update_pwd');
    	
    	$post = post(
    		array(
	    		array('id'				,''	,0),
	    		array('old_passwd'		,''	,''),
	    		array('passwd'			,''	,''),
	    		array('re_passwd'		,''	,'')
    		)
    	);
    	
    	$id			= $post['id'];
    	$old_passwd	= $post['old_passwd'];
    	$passwd		= $post['passwd'];
    	$re_passwd	= $post['re_passwd'];
    	
        import('@.ORG.Admin');
    	$cAdmin = new Admin();

    	//管理员详情
    	$admin = $cAdmin->get_by_id($id);
    	
    	
    	//当前管理员
    	$admin_id = admin_id_ajax();
    	$current_admin = $cAdmin->get_by_id(admin_id_ajax());
    	$role = $current_admin['role'];
    	
    	if($admin['name'] == $passwd){
    		json_error('用户名和密码不能相同');
    		
    	}
    	
        if($passwd == '' || $re_passwd == ''){
        	json_error('密码不能为空');
    		
    	}
    	
        if($passwd != $re_passwd){
        	json_error('两次密码不相同');
    		
    	}
    	
    	//自己修改自己的要验证原密码
    	$can_update = 0;
    	if($id == $admin_id){
    		//是
			$old_pwd_md5 = $cAdmin->passwd_md5($old_passwd);
			
			if($admin['passwd'] != $old_pwd_md5){
				json_error('原密码不正确');
			}else{
				$can_update = 1;
			}
    	}else{
	    	//否
	    	
	    	if($role === 'all'){
	    		$can_update = 1;
	    	}else{
	    		json_error('只有超级管理员才能修改他人密码');
	    	}
    		
    	}
    	//是否超级管理员
    	
    	if($can_update){
			
    		$new_passwd = $cAdmin->passwd_md5($passwd);
    		$upItems = array(
    			'passwd' => $new_passwd
    		);
    		
    		$update_result = $cAdmin->update($id,$upItems);
    		
    		if($update_result){
    			json_success('密码修改成功');
    		}else{
    			json_error('密码修改失败');
    		}
    	}else{
    		json_error('您无权修改密码');
    	}  
    }
    
}